using System;
using Microsoft.SPOT.Hardware;
using Gadgeteer;

/*
 * Original Gadgeteer framework Copyright (c) Microsoft Corporation. (http://gadgeteer.codeplex.com/)
 * 
 * Gadgeteer Light made by Stefan Thoolen (http://gadgeteerlight.codeplex.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace SecretLabs.NETMF.Hardware
{
    internal partial class NetduinoMini : Board
    {
        static NetduinoMini()
        {
            Board.Hardware = new NetduinoMini();
        }

        /// <summary>Translates a socket pin to a Cpu.Pin</summary>
        /// <param name="socket">The Socket #</param>
        /// <param name="pin">The pin on the socket</param>
        /// <returns>The Cpu.Pin</returns>
        public override Cpu.Pin GetPin(int socket, int pin)
        {
            if (socket == 1)
            {
                if (pin == 4) return (Cpu.Pin)0x01; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_11
                if (pin == 5) return (Cpu.Pin)0x00; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_12
                if (pin == 7) return (Cpu.Pin)0x11; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_14
                if (pin == 8) return (Cpu.Pin)0x10; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_15
                if (pin == 9) return (Cpu.Pin)0x12; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_16
            }
            else if (socket == 2)
            {
                if (pin == 3) return (Cpu.Pin)0x0c; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_13
                if (pin == 6) return (Cpu.Pin)0x3e; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_8
                if (pin == 8) return (Cpu.Pin)0x0a; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_9
                if (pin == 9) return (Cpu.Pin)0x0b; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_10
            }
            else if (socket == 3)
            {
                if (pin == 3) return (Cpu.Pin)0x3b; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_5
                if (pin == 4) return (Cpu.Pin)0x3c; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_6
                if (pin == 5) return (Cpu.Pin)0x3d; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_7
                if (pin == 6) return (Cpu.Pin)0x33; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_17
                if (pin == 7) return (Cpu.Pin)0x34; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_18
                if (pin == 8) return (Cpu.Pin)0x35; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_19
                if (pin == 9) return (Cpu.Pin)0x36; // SecretLabs.NETMF.Hardware.NetduinoMini.Pins.GPIO_PIN_20
            }
            return Cpu.Pin.GPIO_NONE;
        }

        /// <summary>Translates a socket to a serial port</summary>
        /// <param name="socket">The Socket #</param>
        /// <returns>The serial port name</returns>
        public override string GetSerial(int socket)
        {
            if (socket == 1)
                return "COM1";

            return "N/A";
        }

        /// <summary>Checks if the socket supports the given type</summary>
        /// <param name="socket">The Socket #</param>
        /// <param name="type">Socket type</param>
        /// <returns>true if it's supported</returns>
        public override bool GetSupported(int socket, char type)
        {
            if (socket == 1)
            {
                if (type == 'S') return true;
                if (type == 'U') return true;
            }
            else if (socket == 2)
            {
                if (type == 'I') return true;
            }
            else if (socket == 3)
            {
                if (type == 'A') return true;
                if (type == 'P') return true;
                if (type == 'X') return true;
                if (type == 'Y') return true;
            }
            return false;
        }

        /// <summary>Creates a new analog input port</summary>
        /// <param name="socket">The socket on which the port exists</param>
        /// <param name="pin">The pin #</param>
        public override AnalogInProvider GetAnalogInProvider(int socket, int pin)
        {
            AnalogInProvider Ret = new AnalogIn();
            Ret.Init(socket, pin);
            return Ret;
        }

        /// <summary>Creates a new PWM output port</summary>
        /// <param name="socket">The socket on which the port exists</param>
        /// <param name="pin">The pin #</param>
        public override PWMOutProvider GetPWMOutProvider(int socket, int pin)
        {
            PWMOutProvider Ret = new PWMOut();
            Ret.Init(socket, pin);
            return Ret;
        }
    }
}
